Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
<System.Drawing.ToolboxBitmap("TCPClient.bmp")> _
Public Class NetworkServer
    Private ServThread As Thread
    Private ServListener As TcpListener
    Private ServClients As New Hashtable()
    Private ServerIP As String = "127.0.0.1"
    Private Prt As Int32 = 5000
    Private AIP As Boolean = False
    Private ServConnected As Boolean = False
    Private ServListen As Boolean = False
    Public Event StatusChanged(ByVal Connected As Boolean, ByVal Listening As Boolean)
    Public Event DataReceived(ByVal Client As Client, ByVal Data As Byte())

    Public Property IP() As String
        Get
            IP = ServerIP
        End Get
        Set(ByVal value As String)
            ServerIP = value
        End Set
    End Property
    Public Property Port() As Integer
        Get
            Port = Prt
        End Get
        Set(ByVal value As Integer)
            Prt = value
        End Set
    End Property
    Public Property AutoIP() As Boolean
        Get
            Return AIP
        End Get
        Set(ByVal value As Boolean)
            AIP = value
        End Set
    End Property
    Public ReadOnly Property ClientCount() As Integer
        Get
            ClientCount = ServClients.Count
        End Get
    End Property
    Public ReadOnly Property Listening() As Boolean
        Get
            Return ServListen
        End Get
    End Property
    Public ReadOnly Property Connected() As Boolean
        Get
            Return ServConnected
        End Get
    End Property
    Private Sub DoListen()
        Dim ipAddress As IPAddress = System.Net.IPAddress.Parse(ServerIP)
        Try
            ServListener = New TcpListener(ipAddress, Prt)
            ServListener.Start()
            ServListen = True
            Do
                Dim x As New Client(ServListener.AcceptTcpClient)
                AddHandler x.Disconnected, AddressOf OnDisconnected
                AddHandler x.DataReceived, AddressOf OnBytesReceived
                ServClients.Add(x.ID, x)
                If ServClients.Count > 0 Then
                    ServConnected = True
                    RaiseEvent StatusChanged(True, True)
                End If
            Loop Until False
        Catch
        End Try
    End Sub
    Private Sub OnDisconnected(ByVal sender As Client)
        ServClients.Remove(sender.ID)
        If ServClients.Count = 0 Then
            ServConnected = False
            ServListen = True
            RaiseEvent StatusChanged(False, True)
        End If
    End Sub

    Private Sub OnBytesReceived(ByVal sender As Client, ByVal data As Byte())
        Dim Out As Byte()
        Out = data
        RaiseEvent DataReceived(sender, Out)
    End Sub
    Public Sub Start()
        If AIP = True Then
            Dim IP As New IPInfo
            ServerIP = IP.IPAddress
        Else
            ServerIP = "127.0.0.1"
        End If
        ServThread = New Thread(AddressOf DoListen)
        ServThread.Start()
        ServListen = True
    End Sub
    Public Sub Terminate()
        If ServClients.Count > 0 Then
            For Each C As Client In ServClients.Values
                C.Dispose()
            Next
            ServClients.Clear()
        End If
        If ServListener IsNot Nothing Then
            ServListener.Stop()
            ServListener = Nothing
        End If
        ServListen = False
    End Sub
    Public Sub Send(ByVal Data As Byte())
        For Each C As Client In ServClients.Values
            C.Send(Data, 0, Data.Length)
        Next
    End Sub
    Public Sub Send(ByVal Receiver As Client, ByVal Data As Byte())
        Receiver.Send(Data, 0, Data.Length)
    End Sub

    Protected Overrides Sub Finalize()
        Terminate()
        MyBase.Finalize()
    End Sub
End Class
